package com.kungfuhacking.wristbandpro.utils;

import android.util.Xml;

import com.kungfuhacking.wristbandpro.chat.bean.EmoBean;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 解析表情brow.xml
 * Created by Aaron on 15/12/30.
 */
public class ParserBrowXml {


    public static LinkedHashMap<String, String> getInfo(InputStream inputStream) {

        LinkedHashMap<String, String> emos = null;

        List<EmoBean> emoBeans = getListInfo(inputStream);

        emos = new LinkedHashMap<String, String>();
        for (int i = 0; i < emoBeans.size(); i++) {
            emos.put(emoBeans.get(i).code, emoBeans.get(i).name);
        }

        return emos;
    }

    public static List<EmoBean> getListInfo(InputStream inputStream) {

        XmlPullParser parser = Xml.newPullParser();
        int eventType = 0;


        List<EmoBean> emoBeans = null;
        EmoBean EmoBean = null;
        try {
            parser.setInput(inputStream, "UTF-8");
            eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:

                        emoBeans = new ArrayList<EmoBean>();

                        break;
                    case XmlPullParser.START_TAG:
                        if ("brow".equals(parser.getName())) {
                            EmoBean = new EmoBean();

                        } else if ("code".equals(parser.getName())) {
                            EmoBean.code = parser.nextText();
                        } else if ("name".equals(parser.getName())) {
                            EmoBean.name = parser.nextText();
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("brow".equals(parser.getName())) {
                            emoBeans.add(EmoBean);
                            EmoBean = null;
                        }
                        break;

                    default:
                        break;
                }

                eventType = parser.next();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

//        emos = new LinkedHashMap<String, String>();
//        for (int i = 0; i < emoBeans.size(); i++) {
//            emos.put(emoBeans.get(i).code, emoBeans.get(i).name);
//        }

        return emoBeans;
    }

}
